var Workport = Workport || {};
//@Path("/Apps")
Workport.AppAPI = (function(){
	var root = WP_RESTFUL_URL + (WP_RESTFUL_URL.endsWith('/') ? '' : '/') + 'Apps/';
	
	var urls = ["Register", "HomeApps", "AppsList", "HomeSearch", "Search", "{1}/AppDetailsById", 
	            "PendingApprovalCount", "EditApps", "{1}/EditAppDetails", "Delete", "PendingApprovalApps", 
	            "Approval", "{1}/AddFavourite", "SaveLike", "{1}/LikedUsers", "LaunchCount","{1}/ShowFeedbacks",
	            "SaveFeedback", "EditCount"];
	
	function getUrl(/*index, replace1, replace2, ...*/) {
		var index, i, loops, r;
		loops = arguments.length;
		if (loops > 0) {
			index = arguments[0];
			r = root + urls[index];
			for (i = 1; i < loops; i++)
				r = r.replace("{"+i+"}", arguments[i]);
		}
		return r;
	}
	
	return {
		// @POST		@Path("/Register")
		// public BasicResponse PostRegisterApp(RegisterAppRequest request)
		registerApp:function(handler,request){
			return sfw.Http.post(getUrl(0),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			},{
				AppId          : request.appId,
				AppTitle       : request.title,
				AppDescription : request.description,
				AppImage       : request.image,
				AppURL         : request.url,
				Mode           : request.mode,
				IsModified     : request.modified
			});
			function toViewModel(data){
				return data;
			}
		},
		// @GET 	@Path("/HomeApps")
		// public HomeAppResponse GetHomeApps()
		getHomeApp:function(handler,request){
			return sfw.Http.get(getUrl(1),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)){
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			},{
				Offset:request.offset,
				Count:request.count,
				Mode:request.mode,
				FilterBy:request.filterBy ? request.filterBy:"0",
				OrderBy:request.orderBy ? request.orderBy :"0"
			});
			function toViewModel(data){
				return data;
			}
		},
		// @GET		@Path("/AppsList")
		// public AppResponse GetApps()
		getAllApp:function(handler,request){
			return sfw.Http.get(getUrl(2),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			},{
				Offset:request.offset,
				Count:request.count,
				FilterBy:request.filterBy,
				orderBy:request.orderBy,
				Mode:request.mode
			});
			function toViewModel(data){
				return data;
			}
		},
		// @GET		@Path("/HomeSearch")
		// public HomeAppResponse GetHomeAppsSearchResult()
		searchHomeApp:function(handler,request){
			return sfw.Http.get(getUrl(3),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			},{
				Offset:request.offset ? request.offset : 0,
				Count:request.count != undefined ? request.count : 10,
				SearchText:request.key,
				OrderBy:request.orderBy,
				Filtermode:request.mode
			});
			function toViewModel(data){
				return data;
			}
		},
		// @GET		@Path("/Search")
		// public AppResponse GetAppsSearchResult()
		searchAllApp:function(handler,request){
			return sfw.Http.get(getUrl(4),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			},{
				Offset:request.offset?request.offset:0,
				Count:request.count != undefined ? request.count:10,
				SearchText:request.key,
				OrderBy:request.orderBy,
				Filtermode:request.mode
			});
			function toViewModel(data){
				return data;
			}
		},
		// @GET		@Path("/{appId}/AppDetailsById")
		// public AppDetailResponse GetAppDetailsByID()
		getDetail:function(handler,request){
			return sfw.Http.get(getUrl(5,request.appId),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			});
			function toViewModel(data){
				return data;
			}
		},
		// @GET		@Path("/PendingApprovalCount")
		// public AppPendingResponse GetPendingApprovalAppsCount()
		getPendingCount:function(handler){
			return sfw.Http.get(getUrl(6),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			});
			function toViewModel(data){
				return data;
			}
		},
		// @GET		@Path("/EditApps")
		// public AppEditResponse GetAppsforEdit()
		getEditApp:function(handler,request){
			return sfw.Http.get(getUrl(7),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			},{
				Offset:request.offset ? request.offset : 0,
				Count:request.count != undefined ? request.count : 10,
				Sort:request.sort
			});
			function toViewModel(data){
				return data;
			}
		},
		// @GET		@Path("/{appId}/EditAppDetails")
		// public AppEditDetailResponse GetEditAppDetails(@PathParam("appId")String appId)
		getEditDetail:function(handler,request){
			return sfw.Http.get(getUrl(8,request.appId),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			});
			function toViewModel(data){
				return data;
			}
		},
		// @POST	@Path("/Delete")
		// public BasicResponse DeleteOrCopy(AppActionRequest request)
		deleteApp:function(handler,request){
			return sfw.Http.post(getUrl(9),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			},{
				AppId  : request.appId,
				Action : request.action
			});
			function toViewModel(data){
				return data;
			}
		},
		// @GET		@Path("/PendingApprovalApps")
		// public AppApprovalResponse GetPendingApprovalApps()
		getPendingApproval:function(handler,request){
			return sfw.Http.get(getUrl(10),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			},{
				PageNo:request.pageNo,
				PageSize:request.pageSize,
				Sort:request.sort
			});
			function toViewModel(data){
				return data;
			}
		},
		// @POST	@Path("/Approval")
		// public BasicResponse PostApproverAction(AppActionRequest request)
		approvalApp:function(handler,request){
			return sfw.Http.post(getUrl(11),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			},{
				AppId  : request.appId,
				Action : request.action
			});
			function toViewModel(data){
				return data;
			}
		},
		// @GET		@Path("/{appId}/AddFavourite")
		// public AddFavouriteResponse GetAddFavourite(@PathParam("appId")String appId)
		addFavourite:function(handler,request){
			return sfw.Http.get(getUrl(12,request.appId),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			});
			function toViewModel(data){
				return data;
			}
		},
		// @POST		@Path("/SaveLike")
		// public AppLikeResponse PostLike(AppLikeRequest request)
		saveLike:function(handler,request){
			return sfw.Http.post(getUrl(13),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			},{
				AppId : request.appId
			});
			function toViewModel(data){
				return data;
			}
		},
		// @GET		@Path("/{appId}/LikedUsers")
		// public AppLikeUserResponse GetAllLikedusers(@PathParam("appId")String appId)
		getLikeUser:function(handler,request){
			return sfw.Http.get(getUrl(14,request.appId),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			},{
				Offset : request.offset,
				Count  : request.count
			});
			function toViewModel(data){
				return data;
			}
		},
		// @POST		@Path("/LaunchCount")
		// public AppLaunchCountResponse PostAddLaunchCount(AppLaunchCountRequest request)
		postLaunchCount:function(handler,request){
			return sfw.Http.post(getUrl(15),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			},{
				AppId : request.appId
			});
			function toViewModel(data){
				return data;
			}
		},
		// @GET 	@Path("/{appId}/ShowFeedbacks")
		// public AppFeedbackDetailResponse GetFeedbackDetails(@PathParam("appId")String appId)
		showFeedback:function(handler,request){
			return sfw.Http.get(getUrl(16,request.appId),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			},{
				Offset  : request.offset,
				Count   : request.count,
				AppType : request.type
			});
			function toViewModel(data){
				return data;
			}
		},
		// @POST	@Path("/SaveFeedback")
		// public AppFeedbackSaveResponse PostFeedback(AppFeedbackRequest request)
		saveFeedback:function(handler,request){
			return sfw.Http.post(getUrl(17),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			},{
				AppId : request.appId,
				FeedbackComments : request.feedback,
				Ratings : request.ratings
			});
			function toViewModel(data){
				return data;
			}
		},
		// @GET		@Path("/EditCount")
		// public AppCountResponse GetEditAppCount()
		getStatus:function(handler){
			return sfw.Http.get(getUrl(18),function(xhr,data){
				if (!sfw.Http.handleError(xhr, data)) {
					if(jQuery.isFunction(handler)){
						handler(data);
					}
				}
			});
			function toViewModel(data){
				return data;
			}
		}
	}
})();
